﻿using System.IO;
using System.IO.Compression;

namespace PhiStore.Experiments.Compression
{
    class BuildInGZip
    {
        public static Stream Compress(byte[] input, int bufferSize)
        {
            MemoryStream output = new MemoryStream();

            GZipStream compressor = new GZipStream(output, CompressionMode.Compress);

            for (int i = 0; i < input.Length; i += bufferSize)
            {
                int length = (int)(i + bufferSize > input.Length ? input.Length - i : bufferSize);

                compressor.Write(input, i, length);
            }

            return output;
        }

        public static Stream Decompress(Stream input, int bufferSize)
        {
            MemoryStream output = new MemoryStream();
            
            GZipStream decompressor = new GZipStream(input, CompressionMode.Decompress);

            byte[] buffer = new byte[bufferSize];
            ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(decompressor, output, buffer);

            return output;
        }
    }
}
